<?php
class SchoolInfo extends Controller {

	function SchoolInfo()
	{
		parent::Controller();
	}
	
	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$this->load->view('header');
		$this->load->view('school-info-form');
		$this->load->view('footer');
	}
	
	function lists()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
	    $data['data'] = $this->SchoolModel->getAll();
		$this->load->view('header');
		$this->load->view('school-list-form', $data);
		$this->load->view('footer');
	}
	
	function detail()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
	    $id = $this->input->post('txtId'); 
	    if($id != null) {
			$data = $this->SchoolModel->getByID($id);
			$this->load->view('header');
			$this->load->view('school-info-form', $data);
			$this->load->view('footer');   	
	    }  else {
	    	$data['data'] = $this->SchoolModel->getAll();
			$this->load->view('header');
			$this->load->view('school-list-form', $data);
			$this->load->view('footer');
	    }   
	}
	
	function search()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
	    $key = $this->input->post('txtTitle');
		$data['data'] = $this->SchoolModel->getByNameLike($key);
		$data['key'] = $key;
		$this->load->view('header');
		$this->load->view('school-list-form', $data);
		$this->load->view('footer');
	}
	
	function save()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
	    $id = $this->input->post('txtSchoolId'); 
	    if($id != null) {
	    	$data = array('school_name' => $this->input->post('txtSchool'), 'school_id' => $id,
	    		'school_detail' => $this->input->post('txtSchoolDeail'));
			$this->SchoolModel->update($data);
			$data = $this->SchoolModel->getByID($id);
			$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
			$this->load->view('header');
			$this->load->view('school-info-form', $data);
			$this->load->view('footer', $msg);   	
	    } else {
	    	$data = array('school_name' => $this->input->post('txtSchool'), 
	    	              'school_detail' => $this->input->post('txtSchoolDeail'));
	    	
			$id = $this->SchoolModel->add($data);
			$data = $this->SchoolModel->getByID($id);
			$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
			$this->load->view('header');
			$this->load->view('school-info-form', $data);
			$this->load->view('footer', $msg);  
	    }	    
	}
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('SchoolModel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$this->SchoolModel->delete($id[$i]);
	    }	    
	    $this->search();
	}
}
?>